package com.yiman.project.system.web;

import com.yiman.framework.web.BaseController;
import com.yiman.framework.web.domain.AjaxResult;
import com.yiman.framework.web.page.TableDataInfo;
import com.yiman.project.system.model.User;
import com.yiman.project.system.service.RoleService;
import com.yiman.project.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author: Mr.yang
 * @create: 2019-06-05
 **/
@Controller
public class UserController extends BaseController {

    @Autowired
    UserService userService;
    @Autowired
    RoleService roleService;
    @RequestMapping("/system/user")
    public String userlist(){
        return "system/user/user";
    }

    @RequestMapping("/addUserPage")
    public String addUser(ModelMap map){
        map.put("roles",roleService.getAllRole());
        return "system/user/add";
    }

    @RequestMapping("/editUserPage/{userId}")
    public String editUser(@PathVariable("userId")Integer userId, ModelMap map){
        map.put("user",userService.getUser(userId));
        map.put("roles",roleService.selectRolesByUserId(userId));
        return "system/user/edit";
    }

    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(User user){
        try {
            userService.editSaveUser(user);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("服务异常");
        }
        return AjaxResult.success("添加成功");
    }

    @RequestMapping("/addUser")
    @ResponseBody
    public AjaxResult addUser(User user){
        try {
            userService.insertUser(user);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("服务异常");
        }
        return AjaxResult.success("添加成功");
    }

    @RequestMapping("/getUserList")
    @ResponseBody
    public TableDataInfo getUserList(User user){
        startPage();
        List<User> list = userService.selectUserList(user);
       return getDataTable(list);
    }

    @RequestMapping("/deleteUser")
    @ResponseBody
    public AjaxResult deleteUser(String ids){
        try {
            userService.deleteUserByIds(ids);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("服务异常");
        }
        return AjaxResult.success("删除成功");
    }
}
